<template>
  <div class="drag" ref="dragDiv">
    <div class="drag_bg"></div>
    <div class="drag_text">{{ confirmWords }}</div>
    <div
      ref="moveDiv"
      @mousedown="mousedownFn($event)"
      :class="{ handler_ok_bg: confirmSuccess }"
      class="handler handler_bg"
      style="position: absolute; top: 0px; left: 0px; z-index: 999"
    >
      <div
        class="iconfont"
        :class="confirmSuccess ? 'ic-circle-correct-fill' : 'ic-doubleright'"
      ></div>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    moveFlag: {
      default: false,
    },
  },
  data() {
    return {
      beginClientX: 0 /*距离屏幕左端距离*/,
      mouseMoveStata: false /*触发拖动状态  判断*/,
      maxwidth: "" /*拖动最大宽度，依据滑块宽度算出来的*/,
      confirmWords: "拖动滑块验证" /*滑块文字*/,
      confirmSuccess: false /*验证成功判断*/,
      domHandler: "",
      domDrag_bg: "",
      domDrag_text: "",
      $message: "",
    };
  },
  methods: {
    //mousedoen 事件
    mousedownFn: function (e) {
      if (!this.moveFlag) {
        ElMessage({
          message: "请输入正确的手机号",
          type: "warning",
        });
        return false;
      }
      if (!this.confirmSuccess) {
        e.preventDefault && e.preventDefault(); //阻止文字选中等 浏览器默认事件
        this.mouseMoveStata = true;
        this.beginClientX = e.clientX;
      }
    },
    //验证成功函数
    successFunction() {
      this.confirmSuccess = true;
      this.confirmWords = "验证通过";
      if (window.addEventListener) {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mousemove", this.mouseMoveFn);
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", this.moseUpFn);
      } else {
        document
          .getElementsByTagName("html")[0]
          .removeEventListener("mouseup", () => {});
      }
      this.domDrag_text.style.color = "#00B42A";
      this.domDrag_text.style.border = "1px solid #00B42A";
      this.domHandler.style.left = this.maxwidth + "px";
      this.domDrag_bg.style.width = this.maxwidth + "px";
      this.$emit("success", true);
    },
    //mousemove事件
    mouseMoveFn(e) {
      if (this.mouseMoveStata) {
        let width = e.clientX - this.beginClientX;
        if (width > 0 && width <= this.maxwidth) {
          this.domHandler.style.left = width + "px";
          this.domDrag_bg.style.width = width + "px";
        } else if (width > this.maxwidth) {
          this.successFunction();
        }
      }
    },
    //mouseup事件
    moseUpFn(e) {
      if (!this.moveFlag) {
        return false;
      }
      this.mouseMoveStata = false;
      var width = e.clientX - this.beginClientX;
      if (width < this.maxwidth) {
        this.domHandler.style.left = 0 + "px";
        this.domDrag_bg.style.width = 0 + "px";
      }
    },
  },
  mounted() {
    this.domHandler = document.getElementsByClassName("handler")[0];
    this.domDrag_bg = document.getElementsByClassName("drag_bg")[0];
    this.domDrag_text = document.getElementsByClassName("drag_text")[0];
    this.maxwidth =
      this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
    document
      .getElementsByTagName("html")[0]
      .addEventListener("mousemove", this.mouseMoveFn);
    document
      .getElementsByTagName("html")[0]
      .addEventListener("mouseup", this.moseUpFn);
  },
};
</script>

<style scoped lang="scss">
.drag {
  position: relative;
  background-color: #f2f3f5;
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 2px;
}
.handler {
  width: 40px;
  height: 40px;
  border: 1px solid map-get($map: $stroke-color, $key: deep);
  cursor: move;
}

.handler_bg {
  position: relative;
  border-radius: 2px 0 0 2px;
  background-color: #f2f3f5;
  div {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    position: absolute;
    right: 0;
    top: 0;
    z-index: 9999;
  }
}
.handler_ok_bg {
  border-color: #00b42a;
  border-radius: 0 2px 2px 0;
  div {
    color: #fff;
    background: #00b42a;
  }
}
.drag_bg {
  background: rgba(0, 180, 42, 0.08);
  height: 40px;
  width: 0px;
}
.drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
</style>
